package com.thj.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.commons.po.RolePo;
import com.dao.BaseDao;
import com.thj.dao.MenuDao;
import com.thj.dao.RoleMenuDao;
import com.thj.dao.UserRoleDao;
import com.thj.po.MenuPo;

public class MenuDaoImpl extends BaseDao<MenuPo>  implements MenuDao {

	@Override
	public List<MenuPo> findByCondition(Map<String, Object> map) {
		StringBuffer sf = new StringBuffer();
		sf.append(" select * from menu where 1=1 ");
		Map<String, String> columnsMap = new HashMap<String, String>();
		// columnsMap.put("is_active", "isactive");//
		return this.executeQuery(sf.toString(), columnsMap, MenuPo.class, null);
	}

	@Override
	public MenuPo findbyId(Integer id) {
		String sql = "select * from menu where menu_id=?";
		Map<String, String> columnsMap = new HashMap<String, String>();
		columnsMap.put("role_id", "id");
		List<MenuPo> list = this.executeQuery(sql, columnsMap, MenuPo.class, id);
		return (list != null && list.size() > 0) ? list.get(0) : null;
	}

	@Override
	public List<MenuPo> findByUId(Integer userId) {
		System.out.println("findByUIT is working");
		RoleMenuDao roleMenuDao = new RoleMenuDaoImpl();
		UserRoleDao userRoleDao = new UserRoleDaoImpl();
		List<RolePo> listRoles = userRoleDao.findRoleById(userId);
		List<MenuPo> list = new ArrayList<MenuPo>();
		System.out.println("List is working");
		for(RolePo rolePo: listRoles){
			//System.out.println(rolePo.getRoleName());
			List<MenuPo> roleList = roleMenuDao.findMenuByRole(rolePo.getRoleId());
			for(MenuPo menuPo:roleList){
				list.add(menuPo);
			}
		}
		if(list == null){
			System.out.println("this is null");
		}
		return list;
	}

}
